﻿/*
版权所有：版权所有(C) 2010，刘建新
文件编号：M01_NewstypeBll.cs
文件名称：NewstypeBll.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2010/12/17
作　　者：
内容摘要：表[CustomerType]业务操作类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Eis51.CampanyCMS.Model.NewsManager;
using System.Web.UI.WebControls;
using MoreUser.Bussiness;
using System.Data;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.Model.NewsManager.QueryO;
using System.Collections;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.FrameWork;
using Eis51.CampanyCMS.Access.NewsManager;

namespace Eis51.CampanyCMS.Business.NewsManager
{
    /// <summary>
	/// CustomerType的BLL 自定义方法
	/// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
	/// </summary>
    public partial class CustomerTypeBLL : BaseBLL<CustomerType>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="listControl"></param>
        public static void BindDropListTreeType(ListControl listControl, string code)
        {
            string condtion = BaseSearchConditonBLL.GetConditon();
            string sql = string.Format("select * from CustomerType where  ( ( Code='{0}' or ''='{0}' )   or ParentID in (select seqno from CustomerType where code='{0}' and {1}))  and {1}", code, condtion);

            DataTable dt = CustomerTypeBLL.GetInstance().GetDataTable(sql);
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add("ParentID");
            dtNew.Columns.Add("title");
            dtNew.Columns.Add("seqno");
            CustomerTypeBLL newstypebll = new CustomerTypeBLL();
            newstypebll.TreeListData("0", dt, dtNew);
            if (dtNew == null || dtNew.Rows.Count <= 0)
            {
                listControl.DataSource = dt;
            }
            else
            {
                listControl.DataSource = dtNew;
            }

            listControl.DataTextField = "title";
            listControl.DataValueField = "seqno";
            listControl.DataBind();
        }


        string symbolf = "";
        string symbol = "";
        public void TreeListData(string parentID, DataTable allTable, DataTable NewTable)
        {
            DataRow[] dataArr = allTable.Select("ParentID=" + parentID);
            int total = dataArr.Length;
            for (int i = 0; i < total; i++)
            {
                if ((i + 1) == total)
                    symbol = "└";
                else
                    symbol = "├";
                DataRow newRow = NewTable.NewRow();
                newRow["ParentID"] = dataArr[i]["ParentID"].ToString();
                newRow["title"] = symbolf + symbol + dataArr[i]["title"].ToString();
                newRow["seqno"] = dataArr[i]["seqno"].ToString();
                NewTable.Rows.Add(newRow);
                if ((i + 1) == total)
                    symbolf += "　";
                else
                    symbolf += "│";
                TreeListData(dataArr[i]["seqno"].ToString(), allTable, NewTable);
                symbolf = symbolf.Substring(0, symbolf.Length - 1);
            }
        }


        public bool Save(CustomerType news)
        {
            news.Lasteupdatedate = DateTime.Now;
            if (news.Seqno == 0)
            {
                news.Createdate = DateTime.Now;
                this.Insert(news);
            }
            else
            {
                this.Update(news);
            }
            return true;
        }


        public bool SubMit(CustomerType news)
        {
            news.Lasteupdateby = AppContext.CurrentAdministrator.UserName;
            news.Lasteupdatedate = DateTime.Now;
            if (news.Seqno == 0)
            {
                news.Createby = AppContext.CurrentAdministrator.UserName;
                news.Createdate = DateTime.Now;
                this.Insert(news);
                if (string.IsNullOrEmpty(news.Code))
                {
                    news.Code = news.Seqno.ToString();
                    this.Update(news);
                }
            }
            else
            {
                this.Update(news);
            }
            BuildPageBLL.CreateHtml(RunType.CustomerType, news);
            return true;
        }

        public DataTable GetTypeByUsername(CustomerTypeQuery query)
        {
            string sql = "select * from CustomerType where Licd={0} and    Createby ='{1}' ";
            sql = string.Format(sql, query.Licd, query.CreateBy);

            return this.GetDataTable(sql);
        }

        public IList Search(string querySql)
        {
            DataTable dt = this.GetDataTable(querySql);
            return CustomerType.Dt2Objs(dt, CustomerType.ColumnNameEnum.DBName);
        }


        public IList Search(CustomerTypeQuery query)
        {
            //string condtion = BaseSearchConditonBLL.GetConditon(true);
            string sql = "select * from CustomerType where 1=1 ";
            sql = string.Format(sql, query.Licd);
            if (query.PublishBeginDate != "")
            {
                sql += " and  CreateDate >= #{0} 00:00:00# ";
                sql = string.Format(sql, query.PublishBeginDate);
            }
            if (query.PublishEndDate != "")
            {
                sql += " and  CreateDate <= #{0} 23:59:59# ";
                sql = string.Format(sql, query.PublishEndDate);
            }
            if (query.NewsTypeName != "")
            {
                sql += " and  title like '%{0}%'  ";
                sql = string.Format(sql, query.NewsTypeName);
            }
            //sql += condtion;

            DataTable dt = this.GetDataTable(sql);
            return CustomerType.Dt2Objs(dt, CustomerType.ColumnNameEnum.DBName);
        }
    }


    #region 代码自动生成
    /// <summary>
    /// CustomerType的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public partial class CustomerTypeBLL : BaseBLL<CustomerType>
    {
        #region 自动生成代码：取得相关实体

        /// <summary>
        /// 方法名称: CustomerTypeBLL
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected CustomerTypeBLL()
        {
        }
        private static volatile CustomerTypeBLL m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 CustomerTypeBLL 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static CustomerTypeBLL GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(CustomerTypeBLL))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new CustomerTypeBLL();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        #endregion

        #region 自动生成代码：基本业务公开函数

        /// <summary>
        /// 方法名称: Get
        /// 内容摘要: 由键值获取一个实体对象
        /// </summary>
        /// <returns>CustomerType</returns>
        public CustomerType Get(CustomerType.Key key)
        {

            return CustomerTypeDao.GetInstance().SelectByID(key);
        }
        /// <summary>
        /// 方法名称: Select
        /// 内容摘要: 基本查询，不带任何条件的查询
        /// </summary>
        public IList Select()
        {
            return CustomerTypeDao.GetInstance().SelectAll();
        }
        /// <summary>
        /// 方法名称: Insert
        /// 内容摘要: 插入一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Insert(CustomerType obj)
        {
            int result = 0;
            try
            {
                result = CustomerTypeDao.GetInstance().Insert(obj);
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        /// <summary>
        /// 方法名称: Update
        /// 内容摘要: 更新一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Update(CustomerType obj)
        {
            CustomerTypeDao.GetInstance().Update(obj);
            return 1;
        }

        /// <summary>
        /// 方法名称: Delete
        /// 内容摘要: 删除一条新纪录
        /// </summary>
        /// <returns>int</returns>
        public int Delete(CustomerType obj)
        {
            bool result = CustomerTypeDao.GetInstance().DeleteByID(new CustomerType.Key(obj.Seqno));
            return result ? 1 : 0;
        }

        #endregion

    }
    #endregion
}